package com.zretc.notice.controller;


import com.zretc.common.entity.R;
import com.zretc.common.entity.Result;
import com.zretc.notice.pojo.Activity;
import com.zretc.notice.service.ActParticipantsService;
import com.zretc.notice.service.ActivityService;
import com.zretc.security.entity.LoginUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * 功能：
 * 作者：111
 * 日期：2024/7/4 22:05
 */

@RestController
public class ActParticipantsController {

    @Resource
    ActParticipantsService actParticipantsService;
    @Resource
    ActivityService activityService;

    @GetMapping("/notice/participants/enroll")
    public Result enroll(Integer activityId){

        Activity activity = activityService.showActivityDetail(activityId);

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser user = (LoginUser) authentication.getPrincipal();

        LocalDate nowDate = LocalDate.now();
        LocalDate stopDate = activity.getEndTime().toLocalDate();

        if(nowDate.isAfter(stopDate)){
            return Result.error("错过报名时间");
        } else {
            List<Integer> records = actParticipantsService.selectActivity(user.getUser().getUserId());
            for(Integer id : records){
                if (id.equals(activityId))
                    return Result.error("请勿多次参加");
            }
            return Result.success(actParticipantsService.enroll(activity.getActivityId(),user.getUser().getUserId()));
        }
    }
}